//
//  HZTopView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/14.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZTopView.h"
#import "MYRadioGroup.h"
#import "HZStockListTableViewCell.h"

#define kMenuH 50
#define kListCellH 59

@interface HZTopView()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_zhangFuTopCache;
    NSArray *_dieFuTopCache;
    NSArray *_huanShouLvTopCache;

}
@property (nonatomic, strong)MYRadioGroup  *pRadioGroup;
@property (nonatomic, strong)UITableView   *pTableView;
@property (nonatomic, strong)NSArray       *pListData;
@property (nonatomic, strong)NSString  *pCurrentTitle;
@end

@implementation HZTopView
//- (NSMutableArray *)pListData
//{
//    if (!_pListData) {
//        _pListData = [NSMutableArray arrayWithCapacity:20];
//    }
//    return _pListData;
//}
#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
#pragma mark -
#pragma mark public
- (void)reloadData
{
    [_pTableView reloadData];
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pRadioGroup = [MYRadioGroup new];
    _pRadioGroup.parent = self;
    _pRadioGroup.backgroundColor = kMYHexToRGB(0xe8e8e8, 1);
    NSArray *titleArray = [NSArray arrayWithObjects:kHZZhangFuTop,kHZDieFuTop,kHZHuanShouLVTop, nil];
    [_pRadioGroup setRadioGroup:titleArray];
    [_pRadioGroup setHiddenSeparateLine:YES andHiddenUnderline:NO];
    [_pRadioGroup setSelectedButton:0];
    
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.dataSource = self;
    _pTableView.delegate = self;
    
    [self addSubview:_pRadioGroup];
    [self addSubview:_pTableView];
    
    [self p_initConstraints];
    
    _pCurrentTitle = kHZZhangFuTop;
}

- (void)p_initConstraints
{
    [_pRadioGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(kMenuH);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pRadioGroup.mas_bottom);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(kListCellH*20);
    }];
}

- (void)p_showRelatedView:(NSString *)title
{
    if ([title isEqual:kHZZhangFuTop]) {
        [self p_loadData:_zhangFuTopCache];
    }else if ([title isEqual:kHZDieFuTop]) {
        [self p_loadData:_dieFuTopCache];
    }else if ([title isEqual:kHZHuanShouLVTop]) {
        [self p_loadData:_huanShouLvTopCache];
    }
    _pCurrentTitle = title;
}

- (void)p_loadData:(NSArray *)data
{
    if (data) {
        self.pListData = data;
        [_pTableView reloadData];
    }
}

- (void)p_sendInfoWithWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter
{
    if (![objc isKindOfClass:[NSNumber class]]) {
        return;
    }
    kHZApiType nApiType = [objc integerValue];
    switch (nApiType) {
        case kHZApi_IndexZhangFuTop_Type:
        {
            if (paramter && [paramter isKindOfClass:[NSArray class]]) {
                _zhangFuTopCache = paramter;
                if ([_pCurrentTitle isEqual:kHZZhangFuTop]) {
                    [self p_loadData:_zhangFuTopCache];
                }
            }
        }
            break;
        case kHZApi_IndexDieFuTop_Type:
        {
            if (paramter && [paramter isKindOfClass:[NSArray class]]) {
                _dieFuTopCache = paramter;
                if ([_pCurrentTitle isEqual:kHZDieFuTop]) {
                    [self p_loadData:_dieFuTopCache];
                }
            }
        }
            break;
        case kHZApi_IndexHuanShouTop_Type:
        {
            if (paramter && [paramter isKindOfClass:[NSArray class]]) {
                _huanShouLvTopCache = paramter;
                if ([_pCurrentTitle isEqual:kHZHuanShouLVTop]) {
                    [self p_loadData:_huanShouLvTopCache];
                }
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.pListData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HZOwnedStockModel *model = _pListData[indexPath.row];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"code" ofType:@"plist"];
    NSArray *codeArray = [[NSArray alloc] initWithContentsOfFile:path];
    if(codeArray.count>indexPath.row){
        NSDictionary *infoDic = codeArray[indexPath.row];
        model.model_Name = NotNilStr(infoDic[@"name"]);
//        model.model_Code =NotNilStr(infoDic[@"code"]);
    }else{
        NSDictionary *infoDic = codeArray[0];
        model.model_Name = NotNilStr(infoDic[@"name"]);
//        model.model_Code =NotNilStr(infoDic[@"code"]);
    }
    HZStockListTableViewCell *cell = [HZStockListTableViewCell ownedStockCellTableView:tableView indexPath:indexPath cellModel:model cellHight:kListCellH];
    
    
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kListCellH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HZOwnedStockModel *model = _pListData[indexPath.row];
    [self.parent upToParentWithObjc:self withType:kHZ_jumpSingleStock_Action withParamter:model];
}
#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_radioGroupClick_Action:
        {
            if ([paramter isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)paramter;
                [self p_showRelatedView:btn.currentTitle];
                [self.parent upToParentWithObjc:self withType:type withParamter:btn];
            }
        }
            break;
            
        default:
            break;
    }
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_updateNewData_Action:
        {
            [self p_sendInfoWithWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        case kHZ_radioGroupClick_Action:
        {
            if ([paramter isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)paramter;
                [_pRadioGroup setSelectedButton:btn.tag];
                [self p_showRelatedView:btn.currentTitle];
                [self.parent upToParentWithObjc:self withType:type withParamter:btn];
            }
        }
            break;
            
        default:
            break;
    }
}

@end
